home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / pd / utilities / tinymeter / source / tinymeter_main / idle.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  5KB  |  217 lines

  1. /*****************************************************************
  2.  
  3.     SystemIdler V1.0 (Idle counter module for TinyMeter 3.6)  by
  4.     Tinic Urou in 1995, FreeWare, orginal code by Thomas Radtke.
  5.     Use this at your own risk. Please leave me a mail if you are
  6.     using this code in your programs:
  7.  
  8.     EMail: tinic@tinic.mayn.sub.de
  9.  
  10.     I modified the code from cpuload2. The routines to count the
  11.     maximum  are  much  more  better I think, since they use the
  12.     exact same  number  of  cycles  as  the  normal  idle  count
  13.     routine.
  14.     On a A4000/030, disabling the startup-sequence, you will now
  15.     get  1%  usage.  With  all my tools I get 9-12% usage. These
  16.     results should be correct.
  17.  
  18.     Bugs: None known.
  19.     ¯¯¯¯
  20.     Invoking:
  21.     ¯¯¯¯¯¯¯¯
  22.     init_idle(); to setup the idle task
  23.     free_idle(); to remove the idle Task
  24.  
  25.     unsigned long idle;      is the actual idlecount
  26.     unsigned long maximum;   is the maximum idlecount
  27.  
  28.     To get f.ex. the percentage of system usage simply use:
  29.  
  30.     __________________________________________________________
  31.  
  32.     extern unsigned long maximum,idle;
  33.  
  34.     showusage()
  35.     {
  36.         int percent,n;
  37.  
  38.         if(init_idle())
  39.         {
  40.         for(n=0;n<25;n++)
  41.         {
  42.             percent=(int)((idle*100)/maximum);
  43.             printf("%d percent free\n",percent);
  44.             Delay(50L);
  45.         }
  46.         free_idle();
  47.         }
  48.     }
  49.  
  50.     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  51.  
  52.     Used compiler:  gcc  2.7.0  with  libnix  1.0  Look  at  the
  53.     makefile for the used options.
  54.  
  55. *******************************************************************/
  56.  
  57. #include <intuition/IntuitionBase.h>
  58. #include <exec/nodes.h>
  59. #include <exec/tasks.h>
  60. #include <libraries/dos.h>
  61. #include <exec/types.h>
  62. #include <exec/memory.h>
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include <signal.h>
  66. #include <exec/libraries.h>
  67. #include <dos/dos.h>
  68.  
  69. extern  struct          Task *FindTask(char *);
  70.     struct          Task *task2,*met;
  71.  
  72.     BOOL            quit_setidle,
  73.             quit_getidle,
  74.             start_count=TRUE;
  75.  
  76.     unsigned long   maximum,
  77.             cnt,
  78.             idle;
  79.  
  80. __geta4 getidle()
  81. {
  82.     struct          timeval updateval;
  83.     struct MsgPort  *timerport;
  84.     struct          timerequest *tr;
  85.     struct          Task *met2;
  86.  
  87.     /* use "__geta4 getidle()" for DICE or SAS and remove geta4(); */
  88.  
  89.     met2=FindTask(NULL);
  90.  
  91.     if ((timerport=(struct MsgPort *)CreatePort (0,0)))
  92.     {
  93.     if ((tr=(struct timerequest *)CreateExtIO(timerport,sizeof(struct timerequest))))
  94.     {
  95.         if ((OpenDevice (TIMERNAME,UNIT_MICROHZ,(struct IORequest *)tr,0))!=0)
  96.         {
  97.         DeleteExtIO(tr);
  98.         DeletePort(timerport);
  99.         goto error;
  100.         }
  101.     }
  102.     else
  103.     {
  104.         DeletePort(timerport);
  105.         goto error;
  106.     }
  107.     }
  108.     else goto error;
  109.  
  110.     updateval.tv_secs =1;
  111.     updateval.tv_micro=0;
  112.  
  113.     while((met2->tc_SigRecvd & SIGBREAKF_CTRL_D)==0)
  114.     {
  115.     /* signal setidle()-task, that we can start counting */
  116.  
  117.     start_count=FALSE;
  118.  
  119.     cnt=0;
  120.     tr->tr_node.io_Command=TR_ADDREQUEST;
  121.     tr->tr_time=updateval;
  122.     DoIO((struct IORequest *)tr);
  123.     idle=cnt;
  124.  
  125.     /* check if we have to setup maximum */
  126.     if(maximum==0)
  127.     {
  128.         maximum=idle;
  129.         Signal(met,SIGBREAKF_CTRL_D);
  130.     }
  131.     }
  132.  
  133.     CloseDevice(tr);
  134.     DeleteExtIO(tr);
  135.     DeletePort(timerport);
  136.  
  137.     error:
  138.  
  139.     quit_getidle=FALSE;
  140.     start_count =FALSE;
  141.  
  142.     idle   =100000;    /* to avoid divisions by zero from the application */
  143.     maximum=100000;
  144.  
  145.     /* Do nothing and wait for DeleteTask() */
  146.  
  147.     Wait(0L);
  148.  
  149. }
  150.  
  151. __geta4 setidle()
  152. {
  153.     char *taskname_1 = "CPU_GET";
  154.     struct Task *task;
  155.  
  156.     /* use "__geta4 setidle()" for DICE or SAS and remove geta4(); */
  157.  
  158.     if(met=FindTask(NULL))
  159.     if(task=(struct Task *)CreateTask(taskname_1,127,getidle,4096))
  160.     {
  161.     quit_getidle=TRUE;
  162.  
  163.     /* Wait for beginning. Allocating a timerequest may take a while */
  164.  
  165.     while (start_count) cnt=0;                                         
  166.  
  167.     /* maximum counter */
  168.     while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  169.     SetSignal(0,SIGBREAKF_CTRL_D);
  170.  
  171.     met->tc_Node.ln_Pri=-127;
  172.  
  173.     /* idle counter */
  174.     while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  175.  
  176.     met->tc_Node.ln_Pri=0;
  177.  
  178.     Signal(task,SIGBREAKF_CTRL_D);
  179.     while(quit_getidle) cnt=0;
  180.  
  181.     /* remove getidle()-task */
  182.  
  183.     Forbid();
  184.     DeleteTask(task);
  185.     Permit();
  186.     }
  187.  
  188.     idle   =100000;    /* to avoid divisions by zero from the       */
  189.     maximum=100000;    /* application, if creation of task failed.  */
  190.  
  191.     quit_setidle=FALSE;
  192.     Wait(0L);
  193. }
  194.  
  195. struct Task *init_idle()
  196. {
  197.     char *taskname_2 = "CPU_SET";
  198.     if( task2=(struct Task *)CreateTask(taskname_2,126,setidle,4096))
  199.     {
  200.     Delay(50L); /* To avoid the use of idle and maximum before they're initialized */
  201.     }
  202.     return(task2);
  203. }
  204.  
  205. free_idle()
  206. {
  207.     quit_setidle=TRUE;
  208.     Signal(task2,SIGBREAKF_CTRL_D);
  209.     while(quit_setidle) Delay(10L);
  210.  
  211.     /* remove setidle()-task */
  212.  
  213.     Forbid();
  214.     DeleteTask(task2);
  215.     Permit();
  216. }
  217.